package edu.xpu.compute.recommend.dataGenerator;

import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import edu.xpu.compute.recommend.entity.SysBook;
import edu.xpu.compute.recommend.entity.SysUser;
import edu.xpu.compute.recommend.entity.SysUserBookRating;
import edu.xpu.compute.recommend.service.SysBookService;
import edu.xpu.compute.recommend.service.SysUserBookRatingService;
import edu.xpu.compute.recommend.service.SysUserService;

@Component
public class SysUserBookRatingGenerator {
	
	@Autowired
	private SysUserBookRatingService sysUserBookRatingService;
	@Autowired
	private SysBookService bookService;
	@Autowired
	private SysUserService userService;
	
	public void ratingGenerator() {
		Date createDate = new Date();
		Random random = new Random();
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 50; j++) {
				SysUserBookRating sysUserBookRating = new SysUserBookRating();
				long userGuid = (long)(i + 1);
				SysUser sysUser = userService.get(userGuid);
				if (sysUser == null) {continue;}
				sysUserBookRating.setUserGuid(userGuid);
				sysUserBookRating.setUserName(sysUser.getName());
				long bookGuid = (long)(j + 1001);
				SysBook sysBook = bookService.get(bookGuid);
				sysUserBookRating.setBookGuid(bookGuid);
				sysUserBookRating.setBookName(sysBook.getName());
				sysUserBookRating.setCreateDate(createDate);
				sysUserBookRating.setRating(random.nextFloat() * (8 - 5) + 5);
				sysUserBookRatingService.save(sysUserBookRating);
			}
		}
	}

}
